package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class StartPane implements ActionListener{

	private JPanel startPane;
	private DefaultListModel listModel; 
	private JButton removeFileOrDirectory, analyze;
	private JList fileList;
	private Gui gui;
	StartPane(Gui gui){
		this.gui = gui;
		startPane = new JPanel();
		
		startPane.setLayout(new GridLayout(3,1));
		
		//file options
		JPanel fileOptions = new JPanel();
		startPane.add(fileOptions);
		
		//file specification
		JPanel fileSpec = new JPanel();
		fileSpec.setLayout(new BorderLayout());
		JLabel fileSpecLbl = new JLabel("F i l e    A n d    D i r e c t o r y    S p e c i f i c a t i o n", JLabel.CENTER);
		fileSpecLbl.setFont(GuiFont.SECTION_HEADING.getFont());
		fileSpec.add(fileSpecLbl, BorderLayout.PAGE_START);
		
		listModel = new DefaultListModel();
		fileList = new JList(listModel);
		fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane fileScrollPane = new JScrollPane(fileList);
		fileSpec.add(fileScrollPane, BorderLayout.CENTER);
		
		JPanel fileSpecOptionsPanel = new JPanel();
		fileSpecOptionsPanel.setLayout(new GridLayout(2,1));
		JButton addFileOrDirectory = new JButton("Add");
		addFileOrDirectory.addActionListener(this);
		
		removeFileOrDirectory = new JButton("Remove");
		removeFileOrDirectory.addActionListener(this);
		removeFileOrDirectory.setEnabled(false);
		
		fileSpecOptionsPanel.add(addFileOrDirectory);
		fileSpecOptionsPanel.add(removeFileOrDirectory);
		
		fileSpec.add(fileSpecOptionsPanel, BorderLayout.LINE_END);
		
		analyze = new JButton("Analyze Files");
		analyze.setEnabled(false);
		analyze.addActionListener(this);
		fileSpec.add(analyze,BorderLayout.PAGE_END);
		
		
		startPane.add(fileSpec);
		
		
		
	}

	public JPanel getPanel(){
		return startPane;
	}
	public void actionPerformed(ActionEvent arg0) {
		String command = arg0.getActionCommand();
		if (command.equals("Add")){
			if (gui.selectFile()){
				listModel.addElement(gui.getSelectedFileOrDirectoryFullPath());
				removeFileOrDirectory.setEnabled(true);
				analyze.setEnabled(true);
			}
			
		}
		else if (command.equals("Remove")){
			int index = fileList.getSelectedIndex();
			if (index != -1){
				listModel.remove(index);

				int size = listModel.getSize();

				if (size == 0) { //Nobody's left, disable firing.
					removeFileOrDirectory.setEnabled(false);
					analyze.setEnabled(false);
				}
			}
		}
		else if (command.equals("Analyze Files")){
			Object[] listArray = listModel.toArray();
			LinkedList<String> stringLinkedList = new LinkedList<String>();
			
			for (Object listItem : listArray){
				stringLinkedList.add(listItem.toString());
			}
			
			gui.analyzeFiles(stringLinkedList.toArray(new String[0]));
		}
		
	}
	
	
}
